package com.hqd527.qdshop.serviceproduct.controller;

import com.hqd527.qdshop.product.BaseAttrInfo;
import com.hqd527.qdshop.product.BaseCategory1;
import com.hqd527.qdshop.product.BaseCategory2;
import com.hqd527.qdshop.product.BaseCategory3;
import com.hqd527.qdshop.result.Result;
import com.hqd527.qdshop.serviceproduct.service.ManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 太优秀
 * @date 2022/2/8 22:45
 * @description
 */

@RestController
@RequestMapping("/admin/product")
@CrossOrigin
@Api(value="商品分类管理",tags = "商品分类管理")
public class BaseManageController {

    @Autowired
    private ManageService manageService;


    /**
     * 查询 一级分类列表
     * @return
     */
    @GetMapping("getCategory1")
    @ApiOperation("获取一级分类列表")
    public Result getCategory1List(){
        List<BaseCategory1> category1List = manageService.getBaseCategory1List();
        return Result.ok(category1List);
    }


    /**
     * 根据 一级分类ID 查询 二级分类列表
     * @param category1Id
     * @return
     */
    @GetMapping("getCategory2/{category1Id}")
    @ApiOperation("根据category1Id查询二级分类列表")
    public Result getCategory2List(@PathVariable("category1Id") Long category1Id){
        List<BaseCategory2> category2List = manageService.getBaseCategory2List(category1Id);
        return Result.ok(category2List);
    }


    /**
     * 根据 二级分类ID 查询 三级分类列表
     * @param category2Id
     * @return
     */
    @GetMapping("getCategory3/{category2Id}")
    @ApiOperation("根据category2Id查询二级分类列表")
    public Result getCategory3List(@PathVariable("category2Id") Long category2Id){
        List<BaseCategory3> category3List = manageService.getBaseCategory3List(category2Id);
        return Result.ok(category3List);
    }


    /**
     * 根据一级分类、二级分类、三级分类获取属性名列表
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("getAttrInfoList/{category1Id}/{category2Id}/{category3Id}")
    @ApiOperation("根据category1Id和category2Id和category3Id获取属性名列表")
    public Result getAttrInfoList(@PathVariable("category1Id") Long category1Id,
                                  @PathVariable("category2Id") Long category2Id,
                                  @PathVariable("category3Id") Long category3Id){
        List<BaseAttrInfo> attrInfoList = manageService.getBaseAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(attrInfoList);
    }


    /**
     * 根据属性id（前端提供）获取对应属性值列表
     * @param attrId
     * @return
     */
    @GetMapping("getAttrValueList/{attrId}")
    @ApiOperation("根据attrId获取属性值列表")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
        BaseAttrInfo baseAttrInfo = manageService.getAttrInfo(attrId);
        return Result.ok(baseAttrInfo);
    }


    /**
     * 将前端传进来的Json数据转换为Object，需要使用@RequestBody注解
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("saveAttrInfo")
    @ApiOperation("保存平台属性")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

}
